package com.gasstion.controller;

import com.gasstion.dao.OilCardDao;
import com.gasstion.dao.UserinfoDao;
import com.gasstion.entity.Goods;
import com.gasstion.entity.OilCard;
import com.gasstion.entity.Userinfo;
import com.gasstion.service.OilCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/oilCard")
@Scope("prototype")
public class OilCardController {

    @Autowired
    private OilCardDao oilCardDao;
    @Autowired
    UserinfoDao userinfoDao;
    @Autowired
    OilCardService oilCardService;

    @RequestMapping("/queryAll")
    @ResponseBody
    public List<OilCard> queryAll(){
        return oilCardDao.queryAll();
    }

    @RequestMapping("/toadd")
    @ResponseBody
    public List<Userinfo> toadd(){
        return userinfoDao.getRoleUser();
    }
    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object> save(OilCard oilCard, int pn){
        oilCardDao.save(oilCard);
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",oilCardService.findAll("",(pn-1) * 2,2)) ;
        map.put("total",oilCardService.findTotalCount("")) ;
        return map;
    }

    @RequestMapping("/querySearch")
    @ResponseBody
    public Map<String,Object> querySearch(String name, int offset, int limit){
        List<OilCard> list = oilCardService.findAll(name,offset,limit) ;
        long count = oilCardService.findTotalCount(name);
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",list) ;
        map.put("total",count) ;
        return  map ;
    }

    @RequestMapping("/chongzhi")
    @ResponseBody
    public Map<String,Object> chongzhi(int balance,int id,int pn){
        oilCardDao.chongzhi(balance,id);
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",oilCardService.findAll("",(pn-1) * 2,2)) ;
        map.put("total",oilCardService.findTotalCount("")) ;
        return map;
    }

}
